﻿using Dapper;
using System.Text;
using System.Collections.Generic;
using Eagles.Base.DataBase;
using Eagles.DomainService.Model.News;
using Eagles.DomainService.Model.ScrollImage;
using Eagles.Interface.DataAccess.ScrollAccess;

namespace Ealges.DomianService.DataAccess.ScrollData
{
    public class ScrollDataAccess : IScrollAccess
    {
        private readonly IDbManager dbManager;

        public ScrollDataAccess(IDbManager dbManager)
        {
            this.dbManager = dbManager;
        }

        public List<TbScrollImage> GetScrollImg(int appId, string pageType)
        {
            return dbManager.Query<TbScrollImage>("select OrgId,PageType,ImageUrl,TargetUrl from eagles.tb_scroll_image where PageType = @PageType and OrgId = @OrgId ",
                new {PageType = pageType, OrgId = appId});
        }

        public List<TbSystemNews> GetScrollNews(string nowDate, string date, int appId, int branchId = 0)
        {
            var sql = new StringBuilder();
            var parameter = new StringBuilder();
            var dynamicParams = new DynamicParameters();
            if (branchId > 0)
                parameter.AppendFormat("and (branchId = 0 or branchId = {0} )", branchId);
            else
                parameter.Append("and branchId = 0 ");
            parameter.Append(" and Status = @Status ");
            dynamicParams.Add("Status", 0);
            parameter.Append(" and OrgId = @OrgId ");
            dynamicParams.Add("OrgId", appId);
            parameter.Append(" and NoticeTime = @NoticeTime1 ");
            dynamicParams.Add("NoticeTime1", nowDate);
            parameter.Append(" or NoticeTime = @NoticeTime2 ");
            dynamicParams.Add("NoticeTime2", date);
            sql.AppendFormat(@"select NewsId,NewsName,NewsContent,NoticeTime,HtmlDesc from tb_system_news where 1=1 {0} ", parameter);
            return dbManager.Query<TbSystemNews>(sql.ToString(), dynamicParams);
        }

        public TbSystemNews GetScrollNewsDetail(int newsId)
        {
            return dbManager.QuerySingle< TbSystemNews>("select NewsId,NewsName,NewsContent,NoticeTime,HtmlDesc from tb_system_news where NewsId=@NewsId ",
                new { NewsId = newsId });
        }
    }
}